/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
  return /^(https?:|mailto:|tel:)/.test(path);
}

/**
   * @param {string} str
   * @returns {Boolean}
   */
export function validUsername(str) {
  // TODO:校验规则
  // const reg = /^[0-9a-zA-Z_]{4,40}$/
  // return reg.test(str)
  return str.length > 0;
}

/**
   * @param {String} str
   * @returns {Boolean}
   */
export function isChineseChar(str) {
  var reg = /[\u4E00-\u9FA5\uF900-\uFA2D]/;
  return reg.test(str);
}

/**
   * @param {string} str
   * @returns {Boolean}
   */
export function validPassword(str) {
  // TODO:校验规则
  // const reg = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{8,16}$/
  // return reg.test(str) && !isChineseChar(str)
  return str.length >= 6 && !isChineseChar(str);
}

// 中国标准时间转化为年月日时分秒
export function transformTimestamp(timestamp) {
  const date = new Date(timestamp);
  const Y = date.getFullYear() + '-';
  const M
      = (date.getMonth() + 1 < 10
        ? '0' + (date.getMonth() + 1)
        : date.getMonth() + 1) + '-';
  const D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' ';
  const h
      = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  const m
      = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes())
      + ':';
  const s
      = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
  const dateString = Y + M + D + h + m + s;
  return dateString;
}

// 获取X天前的时间
export function beforeTimestamp(days) {
  const myDate = new Date();
  const lw = new Date(myDate - 1000 * 60 * 60 * 24 * days); // 最后一个数字30可改，30天的意思
  const lastY = lw.getFullYear();
  const lastM = lw.getMonth() + 1;
  const lastD = lw.getDate();
  const startdate
      = lastY
      + '-'
      + (lastM < 10 ? '0' + lastM : lastM)
      + '-'
      + (lastD < 10 ? '0' + lastD : lastD); // x天之前日期
  return startdate;
}

// 获取现在的时间
export function getCurrentTime() {
  const now = new Date();
  const year = now.getFullYear(); // 得到年份
  let month = now.getMonth() + 1;// 得到月份
  let dateDay = now.getDate();// 得到日期
  let hour = now.getHours();// 得到小时
  let minu = now.getMinutes();// 得到分钟
  let sec = now.getSeconds();// 得到秒
  if (month < 10) { month = '0' + month; }
  if (dateDay < 10) { dateDay = '0' + dateDay; }
  if (hour < 10) { hour = '0' + hour; }
  if (minu < 10) { minu = '0' + minu; }
  if (sec < 10) { sec = '0' + sec; }
  const date = year + '/' + month + '/' + dateDay;
  const dateTime = year + month + dateDay + hour + minu + sec;
  return {
    date: date,
    dateTime: dateTime
  };
}
// 判断内容是否溢出
/**
 * @param {String} str
 * @param {Number} len
 * @return {String}
 */
export function showEllipsis(str, len) {
  if (str && str.length) {
    if (str.length > len) {
      return str.slice(0, len) + '...';
    }
    return str;
  }
  return null;
}
// 获取这是几天或几月前的数据
export function getTimeToDate(timestamp) {
  let flag;
  const nowDate = new Date().getTime();
  const time = new Date(timestamp).getTime();
  const difference = Math.ceil((nowDate - time) / 86400000);
  if (difference < 31) {
    flag = `${difference}天`;
  } else if (difference >= 31 < 365) {
    flag = `${Math.floor(difference / 30)}月`;
  } else {
    flag = `${Math.floor(difference / 365)}年`;
  }
  return flag;
}

// 防抖
export function debounce(time, callback) {
  let timer = null;
  return function () {
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      callback.apply(this, arguments);
    }, time);
  };
}
